#define lv PORTD.RD0
#define lr PORTD.RD1
#define b PORTD.RD2

// LCD module connections
sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D4 at RB0_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D7 at RB3_bit;

sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;
// End LCD module connections

char txt1[] = "Bonjour";
char txt2[] = "Maxime";
char txt3[] = "LCD";
char uart[] = "UART";
char i2c[] = "I2C";
char txt5[] = "Data UART Rx :";
char initchar[] = "                ";
char i,uart1;                              // Loop variable
char uart_rd[17]="                " ;
char barre[] = "|";


void Move_Delay() {                  // Function used for text moving
  Delay_ms(500);                     // You can change the moving speed here
}

void main(){
  PORTD = 0x00;
  TRISD = 0b00000100;
  ANSEL  = 0;                        // Configure AN pins as digital I/O
  ANSELH = 0;
  C1ON_bit = 0;                      // Disable comparators
  C2ON_bit = 0;
  
  lv = 1;
  UART1_Init(9600);               // Initialize UART module at 9600 bps
  Delay_ms(100);                  // Wait for UART module to stabilize
  lv = 0;

  Lcd_Init();                        // Initialize LCD

  Lcd_Cmd(_LCD_CLEAR);               // Clear display
  Lcd_Cmd(_LCD_CURSOR_OFF);          // Cursor off
  Lcd_Out(1,7,txt3);                 // Write text in first row

  Lcd_Out(2,7,uart);                 // Write text in second row
  Delay_ms(2000);
  Lcd_Cmd(_LCD_CLEAR);               // Clear display

  Lcd_Out(1,5,txt1);                 // Write text in first row
  Lcd_Out(2,6,txt2);                 // Write text in second row

  Delay_ms(2000);

  /*// Moving text
  for(i=0; i<4; i++) {               // Move text to the right 4 times
    Lcd_Cmd(_LCD_SHIFT_RIGHT);
    Move_Delay();
  }   */
  Lcd_Cmd(_LCD_CLEAR);    /*
  Lcd_Out(1,5,3);
  Delay_ms(2000); */

  /*Lcd_Out(1,1,txt5);
  uart_rd[0]='T';
  uart_rd[1]='e';
  uart_rd[2]='s';
  uart_rd[3]='t';
  uart_rd[4]=' ';
  uart_rd[5]=0x30;
  uart_rd[6]=49;
  Lcd_Out(2,1,uart_rd);
  Delay_ms(2000);
  Lcd_Out(2,1,initchar);   */
  //UART1_Write(65);
  //UART1_Write(' ');
  /*Lcd_Cmd(_LCD_CLEAR);
  Lcd_Out(1,2,uart);
  Lcd_Out(1,8,barre);
  Lcd_Out(1,9,barre);
  Lcd_Out(2,8,barre);
  Lcd_Out(2,9,barre);
  //Lcd_Out(1,11,i2c);*/

  while(1) {
  Lcd_Cmd(_LCD_CLEAR);
  Lcd_Out(1,2,uart);
  Lcd_Out(1,8,barre);
  Lcd_Out(1,9,barre);
  Lcd_Out(2,8,barre);
  Lcd_Out(2,9,barre);
  //Lcd_Out(1,11,i2c);
    for(i=0;i<16;i++){uart_rd[i]=' ';}
    //Lcd_Out(1,1,txt5);                         // Endless loop
    /*//lr = 1;
    for(i=0; i<8; i++) {             // Move text to the left 7 times
      Lcd_Cmd(_LCD_SHIFT_LEFT);
      Move_Delay();
    }
    //lr = 0;
    for(i=0; i<8; i++) {             // Move text to the right 7 times
      Lcd_Cmd(_LCD_SHIFT_RIGHT);
      Move_Delay();   */
    if (UART1_Data_Ready()) {     // If data is received,
      lv=1;
      //uart = UART1_READ();
      UART1_Read_Text(uart_rd,' ',255);     // read the received data,
      //uart_rd[0] += 48;
      //uart_rd[1] += 48;
      //uart_rd[2] += 48;
      //uart_rd[3] += 48;
      //uart_rd[4] += 48;
      //uart_rd[5] += 48;
      Lcd_Out(2,1,uart_rd);
      lv=0;
      lr=1;
      Delay_ms(500);
      lr=0;


    }
  }
}